# -*- coding: utf-8 -*-
import scrapy
from ..items import BlblItem
import json

class BlSpider(scrapy.Spider):
    name = 'bl'
    allowed_domains = ['bilibili.com']
    start_urls = ['https://www.bilibili.com/ranking/all/0/0/30',
                  'https://www.bilibili.com/ranking/all/1/0/30',
                  'https://www.bilibili.com/ranking/all/168/0/30',
                  'https://www.bilibili.com/ranking/all/3/0/30',
                  'https://www.bilibili.com/ranking/all/129/0/30',
                  'https://www.bilibili.com/ranking/all/4/0/30',
                  'https://www.bilibili.com/ranking/all/36/0/30',
                  'https://www.bilibili.com/ranking/all/188/0/30',
                  'https://www.bilibili.com/ranking/all/160/0/30',
                  'https://www.bilibili.com/ranking/all/119/0/30',
                  'https://www.bilibili.com/ranking/all/155/0/30',
                  'https://www.bilibili.com/ranking/all/5/0/30',
                  'https://www.bilibili.com/ranking/all/181/0/30'
                ]

    def parse(self, response):
        rank_tab=response.xpath('//ul[@class="rank-tab"]/li[@class="active"]/text()').getall()[0]
        print('='*50,'当前爬取榜单为:',rank_tab,'='*50)
        rank_lists=response.xpath('//ul[@class="rank-list"]/li')
        for rank_list in rank_lists:
            rank_num=rank_list.xpath('div[@class="num"]/text()').get()
            title=rank_list.xpath('div/div[@class="info"]/a/text()').get()
            id=rank_list.xpath('div/div[@class="info"]/a/@href').get().split('/av')[-1]
            Detail_link=f'https://api.bilibili.com/x/web-interface/archive/stat?aid={id}'
            Labels_link=f'https://api.bilibili.com/x/tag/archive/tags?aid={id}'
            author=rank_list.xpath('div/div[@class="info"]/div[@class="detail"]/a/span/text()').get()
            score=rank_list.xpath('div/div[@class="info"]/div[@class="pts"]/div/text()').get()
            items={
                'rank_tab':rank_tab,
                'rank_num':rank_num,
                'title':title,
                'id':id,
                'author':author,
                'score':score,
                'Detail_link':Detail_link
            }
            #将api发送给调度器进行详情页的请求，通过meta传递排行页数据
            yield scrapy.Request(url=Labels_link,callback=self.Get_labels,meta={'item':items}, dont_filter=True)
    def Get_labels(self,response):
        items=response.meta['item']
        Detail_link=items['Detail_link']
        html=json.loads(response.body)
        Tags=html['data']
        tag_name=','.join([i['tag_name'] for i in Tags])
        items['tag_name']=tag_name
        yield scrapy.Request(url=Detail_link,callback=self.Get_detail, meta={'item':items},dont_filter=True)
    def Get_detail(self,response):
        items=response.meta['item']
        rank_tab=items['rank_tab']
        rank_num=items['rank_num']
        title=items['title']
        id=items['id']
        author=items['author']
        score=items['score']
        tag_name=items['tag_name']
        html=json.loads(response.body) #解析json数据
        stat=html['data'] #获取详细播放数据
        view=stat['view']
        danmaku=stat['danmaku']
        reply=stat['reply']
        favorite=stat['favorite']
        coin=stat['coin']
        share=stat['share']
        like=stat['like']
        item=BlblItem(
            rank_tab = rank_tab,
            rank_num = rank_num,
            title = title,
            id = id,
            author = author,
            score =score,
            view = view,
            danmaku = danmaku,
            reply = reply,
            favorite = favorite,
            coin = coin,
            share = share,
            like = like,
            tag_name = tag_name
        )
        yield item

